var gDebug = false;
var _mtac = {"performanceMonitor":1,"senseQuery":1};
(function() {
    var mta = document.createElement("script");
    mta.src = "http://pingjs.qq.com/h5/stats.js?v2.0.4";
    mta.setAttribute("name", "MTAH5");
    mta.setAttribute("sid", "500494818");
    mta.setAttribute("cid", "500494820");
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(mta, s);
})();

//调用选择日期
var setDateTime = {
    setDate:function (date_input) {
        $.datetimepicker.setLocale('ch');
        $(date_input).datetimepicker({
            yearOffset: 0,
            lang: 'ch',
            timepicker: false,
            scrollMonth:false,
            scrollTime:false,
            scrollInput:false,
            format: 'Y/m/d',
            formatDate: 'Y/m/d',
            minDate: '2017/01/01', // yesterday is minimum date
            maxDate: '2025/12/31' // and tommorow is maximum date calendar
        });

    },
    setTime:function (time_input) {
        $.datetimepicker.setLocale('ch');
        $(time_input).datetimepicker({
            datepicker: false,
            format: 'H:i',
            scrollMonth:false,
            scrollTime:false,
            scrollInput:false,
            step: 30
        });
    },
    setDateTime:function () {
        $.datetimepicker.setLocale('ch');


    },
    //近三天
    joinThreeDays:function (_this,end_input,start_input) {
        var nowDate = new Date();
        nowDate.toLocaleDateString();
        $(end_input).val(nowDate.toLocaleDateString());
        var myDate = Date.parse(new Date());
        var sjc = myDate - (86400000 * 2);
        var aDate = new Date(sjc);
        $(start_input).val(aDate.toLocaleDateString());
        $(_this).addClass('orange').siblings().removeClass('orange');
    },
    //近七天
    joinSevenDays:function (_this,end_input,start_input) {
        var nowDate = new Date();
        nowDate.toLocaleDateString();
        $(end_input).val(nowDate.toLocaleDateString());
        var myDate = Date.parse(new Date());
        var sjc = myDate - (86400000 * 6);
        var aDate = new Date(sjc);
        $(start_input).val(aDate.toLocaleDateString());
        $(_this).addClass('orange').siblings().removeClass('orange');
    },
}

/**
 * 获取二维码
 * @param object
 * @param url
 */
function qr_code(object, url){
    new QRCode(object, {
        text: url,
        colorDark : "#000000",
        colorLight : "#ffffff",
        correctLevel : QRCode.CorrectLevel.H
    });
}

/**
 * 获取URL的参数
 * @param e
 * @returns {*}
 */
function getQueryString(e) {
    var t = new RegExp("(^|&)" + e + "=([^&]*)(&|$)");
    var a = window.location.search.substr(1).match(t);
    if (a != null) return a[2];
    return ""
}

/**
 * 是否登录
 * @param result
 * @returns {boolean}
 */
function checkAjaxResult(result){
    var code = result.code;
    if(code == -1){
        if(result.msg == '登录过期，请重新登录'){
            //layer.msg(result.msg);
            delCookie("token");
        }
        return false;
    }
    return true;
}

/**
 * 添加cookie
 * @param e
 * @param t
 * @param a
 */
function addCookie(e, t, a) {
    var n = e + "=" + escape(t) + "; path=/";
    if (a > 0) {
        var r = new Date;
        r.setTime(r.getTime() + a * 60 * 1000);
        n = n + ";expires=" + r.toGMTString()
    }
    document.cookie = n
}
/**
 * 获取cookie
 * @param e
 * @returns {null}
 */
function getCookie(e) {
    var t = document.cookie;
    var a = t.split("; ");
    for (var n = 0; n < a.length; n++) {
        var r = a[n].split("=");
        if (r[0] == e) return unescape(r[1])
    }
    return null
}
/**
 * 删除cookie
 * @param e
 */
function delCookie(e) {
    var t = new Date;
    t.setTime(t.getTime() - 1);
    var a = getCookie(e);
    if (a != null) document.cookie = e + "=" + a + "; path=/;expires=" + t.toGMTString()
}

function baseAjax(url, data, succCallback, errCallback, isLoading){
    var _loading = layerLoading.open();
    $.ajax({
        type: "post",
        url: url,
        data: data,
        dataType: "json",
        timeout:60000,
        success: function (result) {
            if (checkAjaxResult(result)) {
                if (typeof succCallback === 'function') {
                    succCallback.call(result);
                }
            }else{
                //登录超时提示
                if (typeof errCallback === 'function') {
                    errCallback.call(result);
                }
            }
            layerLoading.close(_loading);
        },
        error:function(result){
            if (typeof errCallback === 'function') {
                errCallback(result);
            }
            layerLoading.close(_loading);
        }
    })
}

function baseAjaxSync(url, data, callback){
    $.ajax({
        type: "post",
        url: url,
        data: data,
        dataType: "json",
        async:false,
        success: function (result) {
            callback(result);
        }
    })
}

var pageSkip = {
    openActivityDetail: function (id) {
        window.open("/Activity/detail/?id=" + id);
    },
    openMyActivityManager: function (id,anchor) {
        window.open("/MyActivity/MyActivityDetail/?id=" + id +"#"+anchor);
    },    
    openActivityPreview: function (id) {
        window.open("/Activity/preview/?id=" + id);
    },
    openMyActivityIncome: function () {
        window.open("/MyActivity/income/");
    },  
    openMyActivityAccountThird: function () {
        window.open("/MyActivity/account/#third");
    },     
    openEditStudio: function () {
//        window.open("/Studio/index/");
        window.open("/Studio/templateList/");
    },
    openEditStudioById: function (id) {
        window.open("/Studio/index/?id=" + id);
    },
    openOrganizerList: function () {
        window.open("/MyActivity/myOrganizer/");
    },
    openEditStudioByDraftId:function(id){
        window.open("/Studio/index/?draftId=" + id);
    },
    openEditStudioByCopyActivity:function(id){
        window.open("/Studio/index/?id=" + id + "&copy=1");
    },
    openEditStudioByCopyDraft:function(id){
        window.open("/Studio/index/?draftId=" + id + "&copy=1");
    },        

    goToHome:function(){
        window.location.href = '/';
    },
    goToMyHdj: function () {
        window.location.href = '/MyActivity/index/';
    },
    goToBinding: function () {
        window.location.href = '/Index/binding/';
    },
    goToAccount: function () {
        window.location.href = '/MyActivity/account/';
    },
    goToIncome: function () {
        window.location.href = '/MyActivity/income/';
    },
    goToLogout:function(){
        window.location.href = '/Index/logout/';
    },
    goToMyActivityList: function () {
        window.location.href = '/MyActivity/myActivity/';
    },
    goToMyActivityListAnchor: function (anchor) {
        window.location.href = '/MyActivity/myActivity/' + '#' + anchor;
    },
    goToTicketList:function(){
        window.location.href = '/MyActivity/myTicketList/';
    },
    goToTicketListAnchor:function(anchor){
        window.location.href = '/MyActivity/myTicketList/' + '#' + anchor;
    },
    goToMessageList:function(){
        window.location.href = '/Message/index/';
    },
    goToOrganizerList: function () {
        window.location.href = '/MyActivity/myOrganizer/';
    },
    goToTicketChecking:function(){
        window.location.href = '/MyActivity/ticketChecking';
    },
    goToNoticeMessage:function(){
        window.location.href = '/NoticeMessage/index';
    },    
    goToMyActivityDetail: function (id) {
        window.location.href = "/MyActivity/myActivityDetail/?id=" + id;
    },
    goToMyActivityDetailAnchor: function (id,anchor) {
        window.location.href = "/MyActivity/myActivityDetail/?id=" + id + '#' +anchor;
    },
    goToLogin: function () {
        window.location.href = "/Index/login";
    },
    goToRegister: function () {
        window.location.href = "/Index/register";
    },
    goToForgetPwd: function () {
        window.location.href = "/Index/forget";
    },
    //第三方账号绑定页面
    goToThirdAccount: function(){
        window.location.href = "/MyActivity/account/#third";
    },
    exportList: function (activityId) {
        window.location.href = "/MyActivity/exportList/?id=" + activityId + '&type=1';
    },

    reload: function () {
        location.reload();
    }
};

var logs = {
    console: function (log) {
        if (gDebug) {
            console.log(log);
        }
    },
    alert: function (log) {
        if (gDebug) {
            alert(log);
        }
    }
};


var loadJs = function(_url){
    document.write('<script type="text/javascript" src="'+_url+'"></script>');
}

var loadCss = function(_url){
    //<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
    document.write('<link rel="stylesheet" href="'+_url+'"/>');
}

//关注公众号弹窗
var openFollowWechat = function () {
    var _html = "" +
        "<div class='lay-gzh'>" +
            "<p>为了给您提供完整的服务，请先关注公众号</p>" +
            "<section class='login-code login-way'>" +
                "<div class='login-code-box'>" +
                    "<img id='wx_binding_qr' class='login-qrcode' src='' alt=''>" +
                    "<img class='login-saomiaoxian' src='/Public/image/www/saomiaoxian.png' alt=''>" +
                "</div>" +
                "<div class='login-code-wechat'>微信扫码关注</div>" +
            "</section>" +
            "<div class='lay-gzh-footer'>关注公众号将自动为您的账号绑定微信</div>" +
        "</div>";


    layer.open({
        type: 1,
        skin: 'layui-gzh', //加上边框
        area: ['auto', 'auto'], //宽高
        content: _html,
        closeBtn:0,
        title:false,
        success:function () {
            $('.l-content').hide();
        }
    })
}

//返回顶部
function scrollBackTop() {
    $('body').append("<div class='back-top-btn'>" + "<img src='/Public/image/www/backtop-icon.png' alt=''>" + "</div>");
    $(window).scroll(function () {
        var scrollTop = $(window).scrollTop();
        if(scrollTop > 600){
            $('.back-top-btn').show();
        }else {
            $('.back-top-btn').hide();
        }
    });

    $('.back-top-btn').on('click',function () {
        $('html,body').animate({scrollTop:0},300);
    });
}

//浏览器兼容
function isIE(){
    if (window.navigator.userAgent.indexOf("MSIE")>=1){
        var _html = '' +
            '<div class="lay-ie-box">' +
            '<p class="lay-ie-p1">您的浏览器版本过低<p/>' +
            '<p class="lay-ie-p2">为了更好的浏览体验，建议您使用以下现代浏览器的最新版本</p>' +
            '<div class="lay-ie-imgs">' +
            '<a href="http://rj.baidu.com/soft/detail/14744.html?ald">' +
            '<div>' +
            '<img src="/Public/image/www/chrome-icon.png" alt="">' +
            '<p>谷歌浏览器</p>'+
            '</div>' +
            '</a>' +
            '   <a href="http://rj.baidu.com/soft/detail/11843.html?ald">' +
            '<div>' +
            '<img src="/Public/image/www/firefox-icon.png" alt="">' +
            '<p>火狐浏览器</p>' +
            '</div>' +
            '</a>' +
            '</div>' +
            '</div>';

        layer.open({
            skin:'layer-ie',
            type: 1,
            area: ['auto', 'auto'], //宽高
            content: _html,
            title: false,
            btn:false,
            closeBtn:false,
            scrollbar:false,
            yes:function (index) {
                layer.close(index)
            }
        });
        return false;
    }else{
        return true;
    }

}

//鼠标移上去 插入二维码显示
function hoverShowCode(el,codeSrc) {
    $(el).hover(function () {
        $(this).css({'position':'relative','cursor':'pointer'});
        $(this).append("" +
            "<div class='hover-push-qrcode'>" +
            '<img src="'+ codeSrc +'">'+
            '<p>微信扫一扫</p>' +
            "</div>")
    },function () {
        $(el).find('.hover-push-qrcode').remove();
    })
}

//loading
var layerLoading = {
    open:function (config) {
        config = config ||  {shade:0, time:10000};
        var _loading = layer.load(0,config);
        return _loading;
    },
    close:function(_loading){
        layer.close(_loading);
    }
}

function layerClose() {
    layer.closeAll();
}

//chrome 谷歌浏览器 兼容提示 全局可调用
function isChrome() {
    //todo 传参判断

    // for(var i=0; i < browser.length; i++){
    //     if(browser[i] == 'ie'){
    //
    //     }else if(browser[i] == 'chrome'){
    //
    //     }
    // }

    var _html = "" +
        "<div class='chrome-div'>" +
            "<div class='chrome-left'>" +
                "<img src='/Public/image/www/chrome-icon.png' alt=''>" +
            "</div>" +
            "<div class='chrome-right'>" +
                "<span> 您当前使用的浏览器可能会出现布局和功能异常，建议使用新版谷歌浏览器，</span>" +
                "<span>" +
                    "<a class='f66926 pointer' href='http://rj.baidu.com/soft/detail/14744.html?ald'>立即下载。</a>" +
                "</span>" +
                "<span class='f66926 chrome-close hover-scale pointer' onclick="+ "$(this).parents('.chrome-div').remove();>" + "×</span>" +
            "</div>" +
        "</div>";

    var isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
    if (isChrome) {
    } else {
        console.log('非chrome浏览器，兼容性可能存在问题')
        $('html>body').append(_html);
    }
}

function browserTip(browsers, tip){
    tip = tip || '您当前使用的浏览器可能会出现功能异常，建议使用新版谷歌浏览器';
    var _html = "" +
        "<div class='chrome-div'>" +
        "<div class='chrome-left'>" +
        "<img src='/Public/image/www/chrome-icon.png' alt=''>" +
        "</div>" +
        "<div class='chrome-right'>" +
        "<span>" + tip + "</span>" +
        "<span>" +
        "<a class='f66926 pointer' href='http://rj.baidu.com/soft/detail/14744.html?ald'>立即下载。</a>" +
        "</span>" +
        "<span class='f66926 chrome-close hover-scale pointer' onclick="+ "$(this).parents('.chrome-div').remove();>" + "×</span>" +
        "</div>" +
        "</div>";

    var isTip = false;
    for (var i = 0; i < browsers.length; i++) {
        if (browsers[i] == 'IE') {
            isTip = window.navigator.userAgent.indexOf("MSIE") !== -1;
            if(!!window.ActiveXObject || "ActiveXObject" in window || isTip){ // IE11的判断
                isTip =  true;
            }
        } else if (browsers[i] == 'Chrome') {
            isTip = window.navigator.userAgent.indexOf("Chrome") !== -1;
        }else if (browsers[i] == 'Firefox') {
            isTip = window.navigator.userAgent.indexOf("Firefox") !== -1;
        }
        if(isTip){
            $('html>body').append(_html);
        }
    }

}

function backToTop() {
    $('html,body').animate({scrollTop:0},200);
}

//浏览器右下角的悬浮按钮
function fixedRightBtn() {
    var threeBtn = "" +
        "<div class='fixed-right-btn'>" +
            
            "<div class='fixed-right-btn-box'>" +
            
                "<div class='fixed-btn fixed-btn-1'>" +
                    "<span class='fa fa-question-circle-o fa-2x'></span>" +
                "</div>" +
            
                "<div class='fixed-content fixed-content-kefu none'>" +
                    "<p>客服支持</p>" +
                    "<div class='fixed-flex-div'>" +
                        "<div class='fixed-flex-div-left'>" +
                            "<img src='../../../images/mgt/myActivity/hdj-kefu-code.png' alt=''>" +
                            "<p class='f66926' style='font-size: 12px;margin-top: 10px'> 扫一扫，添加微信客服</p>" +
                        "</div>" +
                        "<div class='fixed-flex-div-right'>" +
                            "<div>" +
                                "<img src='../../../images/mgt/myActivity/fixed-qq.png' alt=''>" +
                                "<span>3549203715</span>" +
                            "</div>  " +
                            "<div>" +
                                "<img src='../../../images/mgt/myActivity/fixed-emain.png' alt=''>" +
                                "<span>kf@huodongju.com</span>" +
                            "</div>  " +
                            "<div>" +
                                "<img src='../../images/mgt/myActivity/fixed-question.png' alt=''>" +
                                "<span><a href='/Public/static/www/help_center.html' target='_blank'>常见问题</a></span>" +
                            "</div>  " +
                        "</div>" +
                    "</div>" +
                "</div>" +
            
                "<div class='fixed-btn fixed-btn-2'>" +
                    "<span class='fa fa-handshake-o fa-lg'></span>" +
                "</div>" +
            
                "<div class='fixed-content fixed-content-shangwu none'>" +
                    "<p>商务合作</p>" +
                    "<div class='fixed-flex-div'>" +
                        "<div class='fixed-flex-div-left'>" +
                            "<img src='../../../images/mgt/myActivity/hdj-shangwu-code.png' alt=''>" +
                            "<p class='f66926' style='font-size: 12px;margin-top: 10px'> 扫一扫，添加商务微信</p>" +
                        "</div>" +
                        "<div class='fixed-flex-div-right'>" +
                            "<div>" +
                                "<img src='../../../images/mgt/myActivity/fixed-qq.png' alt=''>" +
                                "<span>3028814192</span>" +
                            "</div>  " +
                            "<div>" +
                                "<img src='../../../images/mgt/myActivity/fixed-emain.png' alt=''>" +
                                "<span>bd@huodongju.com</span>" +
                            "</div>  " +
                        "</div>" +
                    "</div>" +
                "</div>" +
            
                "<div class='fixed-btn'>" +
                    "<span onclick='backToTop();' class='fa fa-arrow-up fa-lg'></span>" +
                "</div>" +
            "</div>" +
        "</div>";

    $('html body').append(threeBtn);
    $('.fixed-btn-1').click(function () {
        $('.fixed-content-shangwu').hide();
        $('.fixed-content-kefu').slideToggle();

    });
    $('.fixed-btn-2').click(function () {
        $('.fixed-content-kefu').hide();
        $('.fixed-content-shangwu').slideToggle();
    });
    $('#right-bottom-content').click(function () {
        $('.fixed-content').slideUp();
    })
}

//倒计时
function hdjTimer(btn,_numId,_num) {
    var $this = $(btn);
    $(_numId).text(_num);
    var num = parseInt(_num);
    $this.hide().siblings().show();
    var timer = setInterval(function () {
        $(_numId).text(num --);
        if (num == -1){
            $this.show().siblings().hide();
            clearInterval(timer);
        }
    },1000);
}

//箭头绑定点击功能（主要是用于管理中心）
function arrowClick(_this) {
    $(_this).prev().focus();
    $(_this).prev().trigger('click');
}

/**
 * 验证码刷新
 * @param _this
 */
function verifyCodeRefresh(_this){
    $(_this).attr('src', '/Index/getVerifyCode/' + Math.random());
}

//局部滚动时候禁止外层的浏览器窗口滚动
// function bodyStopScroll(el) {
//     $(el).hover(function () {
//         $('html,body').css({
//             'overflow':'hidden',
//             'padding-right':'10px'
//         });
//     },function () {
//         $('html,body').css('overflow-y','');
//     })
// }